<template>
  <div class="layui-container fly-marginTop">
    <div class="fly-panel fly-panel-user" pad20>
      <div class="layui-tab layui-tab-brief" lay-filter="user">
        <ul class="layui-tab-title">
          <li>
            <router-link to="/login">登入</router-link>
          </li>
          <li class="layui-this">
            重置密码
          </li>
        </ul>
        <div class="layui-form layui-tab-content" id="LAY_ucm" style="padding: 20px 0">
          <validation-observer ref="observer" v-slot="{validate}">
            <div class="layui-tab-item layui-show">
              <div class="fly-msg">{{username}}，请重置您的密码</div>
              <div class="layui-form layui-form-pane" style="margin-top: 30px;">
                <form method="post">
                  <div class="layui-form-item">
                    <validation-provider name="password" rules="required|min:6|max:16" v-slot="{errors}">
                      <div class="layui-row">
                        <label for="L_pass" class="layui-form-label">密码</label>
                        <div class="layui-input-inline">
                          <input type="password" name="password" placeholder="6到16个字符" autocomplete="off" v-model="password" class="layui-input">
                        </div>
                        <div class="error layui-form-mid">
                          <span style="color: red;">{{errors[0]}}</span>
                        </div>
                      </div>
                    </validation-provider>
                  </div>
                  <div class="layui-form-item">
                    <validation-provider name="repass" rules="required|confirmed:password" v-slot="{errors}">
                      <div class="layui-row">
                        <label for="L_repass" class="layui-form-label">确认密码</label>
                        <div class="layui-input-inline">
                          <input type="password" name="repass" placeholder="请输入确认密码" autocomplete="off" v-model="repass" class="layui-input">
                        </div>
                        <div class="error layui-form-mid">
                          <span style="color: red;">{{errors[0]}}</span>
                        </div>
                      </div>
                    </validation-provider>
                  </div>
                  <div class="layui-form-item">
                    <validation-provider ref="code" vid="code" name="code" rules="required|length:4" v-slot="{errors}">
                      <div class="layui-row">
                        <label for="L_vercode" class="layui-form-label">验证码</label>
                        <div class="layui-input-inline">
                          <input type="text" name="code" placeholder="请输入验证码" v-model="code" autocomplete="off" class="layui-input" />
                        </div>
                        <div class>
                          <span style="color: #c00;" class="svg" v-html="svg" @click="_getCode()"></span>
                        </div>
                      </div>
                      <div class="error layui-form-mid">
                        <span style="color: red;">{{errors[0]}}</span>
                      </div>
                    </validation-provider>
                    <div class="layui-form-item">
                      <button class="layui-btn" type="button" @click="validate().then(submit)" lay-submit>确认修改</button>
                    </div>
                  </div>

                </form>
              </div>
            </div>
          </validation-observer>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ValidationProvider, ValidationObserver } from 'vee-validate'
import { getCode, reset } from '@/api/login'
import uuid from 'uuid/dist/v4'
export default {
  name: 'reset',
  data () {
    return {
      username: '',
      code: '',
      svg: '',
      key: '',
      password: '',
      repass: ''

    }
  },
  components: {
    ValidationProvider, ValidationObserver
  },
  mounted () {
    const queryStr = window.location.href.replace(/.*\?/, '')
    const obj = Object.fromEntries(queryStr.split('&').map(v => (
      v.split('=')
    )))
    this.username = decodeURIComponent(obj.username)
    this.key = decodeURIComponent(obj.key)
    let sid = ''
    if (localStorage.getItem('sid')) {
      sid = localStorage.getItem('sid')
    } else {
      sid = uuid()
      localStorage.setItem('sid', sid)
    }
    this.$store.commit('setSid', sid)
    this._getCode()
  },
  methods: {
    _getCode () {
      const sid = this.$store.state.sid
      getCode(sid).then(res => {
        if (res.code === 200) {
          this.svg = res.data
        }
      })
    },
    async submit () {
      const isValid = await this.$refs.observer.validate()
      if (!isValid) {
        return
      }
      const sid = this.$store.state.sid
      reset({
        key: this.key,
        code: this.code,
        sid: sid,
        password: this.password
      }).then((res) => {
        console.log(res)
        if (res.code === 200) {
          this.$alert('密码修改成功')
          setTimeout(() => {
            this.$router.push('/login')
          }, 1000)
        } else {
          this._getCode()
          if (res.msg instanceof Object) {
            this.$refs.observer.setErrors(res.msg)
          } else {
            this.$alert(res.msg)
          }
        }
      }).catch((err) => {
        const data = err.response.data
        if (data.code === 500) {
          this.$alert('服务器链接错误')
        }
        console.log(err)
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.svg {
  position: relative;
  top: -4px;
}
</style>
